package controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

import data.Data;


@SuppressWarnings("serial")
public class OwerServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        Date date_cache = new Date(new Date().getTime()+60*60*1000);
		
        resp.setHeader( "Cache-Control", "public, max-age=86400");
		resp.setHeader( "Expires", new Date(Calendar.getInstance().getTime().getTime()+24*60*60*1000).toGMTString());
        
        String path = ((HttpServletRequest)req).getRequestURI();
		StringTokenizer st = new StringTokenizer( path,"/");
        int count = st.countTokens(); 

        if(count != 2)
        {
        	resp.sendRedirect("/");
        }
        
		// skip one token /question/abcd (remove sites)
		st.nextToken();
		String title_url = st.nextToken();
        
		int rand = new Random().nextInt(1000000);
		DBCollection collection = Data.data("domain");
		DBCursor cursor = collection.find(new BasicDBObject("random",new BasicDBObject("$lt",rand))).limit(40);
		
		req.setAttribute("cursor", cursor);
		req.setAttribute("email", "Owner : "+title_url);
		
		req.setAttribute("title", "Owner - "+title_url);
		req.setAttribute("description", "Owner - "+title_url+"the website of the same Owner "+title_url+" is synthesized by  "+Utils.ValidDomain());
		req.setAttribute("keyword", title_url+",linkout,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info,topic");
		
		try {
			req.getRequestDispatcher("/created.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
